package com.wfc.servlet.user;

import com.wfc.entity.Dish;
import com.wfc.entity.User;
import com.wfc.service.DishService;
import com.wfc.service.UserService;
import com.wfc.service.serviceImpl.DishServiceImpl;
import com.wfc.service.serviceImpl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

//@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("user1");
        String password = req.getParameter("pass");
        String tenDayLogin = req.getParameter("tenDayLogin");
        HttpSession session = req.getSession();

        //获取用户信息
        UserService userService = new UserServiceImpl();
        User news_user1 = userService.ServiceByUsername(username);
        String avatar = news_user1.getAvatar();

//        //获取所有菜单信息
//        DishService dishService = new DishServiceImpl();
//        List<Dish> dishByTitle = dishService.getDishByTitle();
//        //session存所有菜品集合
//        session.setAttribute("dishList",dishByTitle);

        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html; charset=utf-8");
        PrintWriter writer = resp.getWriter();
        String mess="";
        if(password.equals(news_user1.getPassword()) ){
            //session存用户信息
            session.setAttribute("user",news_user1);
            mess="登录成功";
            if("ok".equals(tenDayLogin)) {//说明用户选择了十天内免登陆
                //创建Cookie对象

                Cookie cookie1 = new Cookie("username",username);
                Cookie cookie2 = new Cookie("password",password);
                //设置有效时间
                cookie1.setMaxAge(60 * 60 * 24 * 10);
                cookie2.setMaxAge(60 * 60 * 24 * 10);
                //设置关联路径,默认根路径就行
                cookie1.setPath(req.getContextPath());
                cookie2.setPath(req.getContextPath());
                //发送Cookie给浏览器
                resp.addCookie(cookie1);
                resp.addCookie(cookie2);
            }

//          resp.sendRedirect(req.getContextPath()+"/index.jsp");
        }else {
            mess="登录失败";
            System.out.println("登录失败，请检查用户名或者密码！");
//          resp.sendRedirect(req.getContextPath()+"/Login.jsp");
        }
        writer.println(mess);
        writer.flush();
        writer.close();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        String name=req.getParameter("username");
//        String pass = req.getParameter("password");
//
//        UserService userService = new UserServiceImpl();
//        List<User> newsList = userService.ServiceSelectAll();
//        resp.setContentType("text/html;charset=utf-8");
//        PrintWriter printWriter=resp.getWriter();
//        User user = new User();
//        String mag="1";
//        for (int i=0;i<newsList.size();i++){
//            if (newsList.get(i).getPhone().equals(name)&&newsList.get(i).getPassword().equals(pass)){
//                mag="1";
//                break;
//            }else {
//                mag="0";
//            }
//        }
//        //resp.setCharacterEncoding("text/html;charset=UTF-8");
//        printWriter.println(mag);
//        printWriter.flush();
//        printWriter.close();
    }
}
